﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using SharpPcap;
using PacketDotNet;

namespace Dossert
{
    public class SynFlooderInfo
    {
        public LivePcapDevice PCapDevice { get; set; }

        public IPAddress TargetIP { get; set; }
        public IPAddress SourceIP { get; set; }
        public ushort TargetPort { get; set; }
        public ushort SourcePort { get; set; }

        public PhysicalAddress SourceMAC { get; set; }
        public PhysicalAddress TargetMAC { get; set; }

        #region Constructing
        public SynFlooderInfo(LivePcapDevice dev, IPAddress targetIP, ushort targetPort, IPAddress sourceIP = null, ushort sourcePort = 1337)
        {
            Init(dev, targetIP, targetPort, sourceIP ?? dev.Addresses[1].Addr.ipAddress, sourcePort);
        }

        public SynFlooderInfo(LivePcapDevice dev, string targetIP, ushort targetPort, IPAddress sourceIP = null, ushort sourcePort = 1337)
        {
            IPAddress target = IPAddress.None;
            try
            {
                target = IPAddress.Parse(targetIP);
            }
            catch
            {
                target = IPAddress.None;
            }

            Init(dev, target, targetPort, sourceIP ?? dev.Addresses[1].Addr.ipAddress, sourcePort);
        }

        public SynFlooderInfo(LivePcapDevice dev, string targetIP, ushort targetPort, string sourceIP, ushort sourcePort = 1337)
        {
            IPAddress target = IPAddress.None;
            IPAddress source = IPAddress.Broadcast;
            try
            {
                target = IPAddress.Parse(targetIP);
            }
            catch
            {
                target = IPAddress.None;
            }

            try
            {
                source = IPAddress.Parse(sourceIP);
            }
            catch
            {
                source = dev.Addresses[1].Addr.ipAddress;
            }

            Init(dev, target, targetPort, source, sourcePort);
        }

        private void Init(LivePcapDevice dev, IPAddress targetIP, ushort targetPort, IPAddress sourceIP, ushort sourcePort)
        {
            PCapDevice = dev;

            TargetIP = targetIP;
            SourceIP = sourceIP;

            TargetPort = targetPort;
            SourcePort = sourcePort;

            //TODO: Implement real MAC codes
            try
            {
                SourceMAC = dev.Addresses[2].Addr.hardwareAddress;
            }
            catch
            {
                SourceMAC = PhysicalAddress.Parse("DE-AD-BE-EF-13-37");
            }

            TargetMAC = PhysicalAddress.Parse("13-37-13-37-13-37");
        }
        #endregion
    }
}
